#pragma  once


namespace art
{
	inline HRESULT xmlWriteRect(IXMLDOMNode *pNode, LPCRECT lpszRect)
	{
		CString strBuffer;
		strBuffer.Format(_T("(%d,%d,%d,%d)"), lpszRect->left, lpszRect->top, lpszRect->right, lpszRect->bottom);		
		return pNode->put_text(strBuffer.AllocSysString());
	}


	inline HRESULT xmlReadRect(IXMLDOMNode *pNode, RECT &rect)
	{
		CComBSTR bstrText;
		HRESULT	hr = E_FAIL;

		hr = pNode->get_text(&bstrText);
		if (bstrText.Length() > 0)
			_stscanf(bstrText, _T("(%d, %d, %d, %d)"), &rect.left, &rect.top, &rect.right, &rect.bottom);

		return hr;
	}


	inline HRESULT xmlWriteVersion(IXMLDOMNode *pNode, VS_FIXEDFILEINFO &vsffi)
	{
		CString strBuffer;
		strBuffer.Format(_T("(%d,%d,%d,%d)"),
			HIWORD(vsffi.dwFileVersionMS),
			LOWORD(vsffi.dwFileVersionMS),
			HIWORD(vsffi.dwFileVersionLS),
			LOWORD(vsffi.dwFileVersionLS));
		return pNode->put_text(strBuffer.AllocSysString());
	}


	inline HRESULT xmlReadVersion(IXMLDOMNode *pNode, VS_FIXEDFILEINFO &vsffi)
	{
		CComBSTR bstrText;
		HRESULT	hr = E_FAIL;
		DWORD dwHIMS =0;
		DWORD dwLOMS = 0;
		DWORD dwHILS = 0;
		DWORD dwLOLS = 0;

		hr = pNode->get_text(&bstrText);
		if (bstrText.Length() > 0)
		{
			_stscanf(bstrText, _T("(%d, %d, %d, %d)"), &dwHIMS, &dwLOMS, &dwHILS, &dwLOLS);
			vsffi.dwFileVersionMS	= MAKELONG(dwLOMS, dwHIMS);
			vsffi.dwFileVersionLS	= MAKELONG(dwLOLS, dwHILS);
		}

		return hr;
	}


	inline HRESULT WINAPI xmlAddNode(__in IXMLDOMDocument2 *pXmlDoc,
										__in IXMLDOMNode *pParentNode,
										__in BSTR bstrNodeName,
										__in BSTR bstrNodeValue,
										__out IXMLDOMNode **pRetNode)
	{
		CComQIPtr<IXMLDOMNode>	pNewNode;
		CComBSTR				bstrNamespaceURI;
		CComVariant				varNodeType;
		HRESULT					hr = 0;

		varNodeType		= NODE_ELEMENT;
		bstrNamespaceURI= "";

		hr = pXmlDoc->createNode(varNodeType, bstrNodeName, bstrNamespaceURI, &pNewNode);
		hr = pParentNode->appendChild(pNewNode, pRetNode);
		COM_ASSERT(hr);
		if (SUCCEEDED(hr))
			hr = (*pRetNode)->put_text(bstrNodeValue);

		return hr;
	}

	inline HRESULT WINAPI xmlReadNodeText(__in IXMLDOMElement *pElement,
											__in BSTR bstrNodeNamePath,
											__out BSTR *bstrNodeValue)
	{
		CComQIPtr<IXMLDOMNode>	pOutNode;
		HRESULT hr = E_FAIL;

		hr = pElement->selectSingleNode(bstrNodeNamePath, &pOutNode);
		if (NULL != pOutNode)
			hr = pOutNode->get_text(bstrNodeValue);
		return hr;
	}

	inline HRESULT WINAPI xmlWriteNodeText(__in IXMLDOMElement *pElement,
											__in BSTR bstrNodeNamePath,
											__in BSTR bstrNodeValue)
	{
		CComQIPtr<IXMLDOMNode>	pOutNode;
		HRESULT	hr = E_FAIL;

		hr = pElement->selectSingleNode(bstrNodeNamePath, &pOutNode);
		if (NULL != pOutNode)
			hr = pOutNode->put_text(bstrNodeValue);
		return hr;
	}
};

HRESULT WINAPI xmlAddNodeType(__in IXMLDOMDocument2 *pXmlDoc,
							  __in IXMLDOMNode *pParentNode,
							  __in BSTR bstrNodeName,
							  __in VARIANT &varNodeValue,
							  __out IXMLDOMNode **pRetNode);
